# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from ui_mainWindow import Ui_MainWindow


class MainWindow(QtGui.QMainWindow):
	def __init__(self, controller):
		QtGui.QMainWindow.__init__(self)

		self.controller = controller

		self.editFrame = None
		self.searchFrame = None
		self.detailsFrame = None

        # Set up the user interface from Designer.
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)

		self.mainSplitter = QtGui.QSplitter()

		self.horizSplitter = QtGui.QSplitter(QtCore.Qt.Vertical)

		self.mainSplitter.addWidget(self.horizSplitter)

		self.setCentralWidget(self.mainSplitter)

		self.compoundMenu = QtGui.QMenu("Compound")
		self.actionAddNewCompound = self.compoundMenu.addAction("Add New Compound", self.controller.newCompoundAction)
		self.actionSearchCompound = self.compoundMenu.addAction("Search Compound", self.controller.searchCompoundAction)

		self.reactionMenu = QtGui.QMenu("Reaction")
		self.actionAddNewReaction = self.reactionMenu.addAction("Add New Reaction", self.controller.newReactionAction)

		self.referenceMenu = QtGui.QMenu("Reference")
		self.actionAddNewReference = self.referenceMenu.addAction("Add New Reference", self.controller.newReferenceAction)
		self.actionSearchReference = self.referenceMenu.addAction("Search Reference", self.controller.searchReferenceAction)

		self.viewMenu = QtGui.QMenu("View")
		self.actionInformation = self.viewMenu.addAction("View Information", self.controller.informationAction)
		self.actionViewSearch = self.viewMenu.addAction("View Search", self.controller.viewSearchAction)
		#self.actionDetails = self.viewMenu.addAction("View Details", self.controller.detailsAction)
		self.actionInformation.setCheckable(True)
		self.actionInformation.setChecked(False)
		self.actionViewSearch.setCheckable(True)
		self.actionViewSearch.setChecked(False)
		#self.actionDetails.setCheckable(True)
		#self.actionDetails.setChecked(False)

		self.menuBar().addMenu(self.viewMenu)
		self.menuBar().addMenu(self.compoundMenu)
		self.menuBar().addMenu(self.reactionMenu)
		self.menuBar().addMenu(self.referenceMenu)

		self.connect(self.ui.actionConnect, QtCore.SIGNAL("triggered(bool)"), self.controller.loginAction)
		self.connect(self.ui.actionLogout, QtCore.SIGNAL("triggered(bool)"), self.controller.logoutAction)
		self.connect(self.viewMenu, QtCore.SIGNAL("aboutToShow()"), self.updateViewMenu)


	def viewEditFrame(self, view=False):
		if view or self.actionInformation.isChecked():
			self.actionInformation.setChecked(True)
			self.editFrame.show()
		else:
			self.actionInformation.setChecked(False)
			self.editFrame.hide()

	def viewSearchFrame(self, view=False):
		if view or self.actionViewSearch.isChecked():
			try:
				self.searchFrame.show()
				self.actionViewSearch.setChecked(True)
			except AttributeError:
				self.actionViewSearch.setChecked(False)
		else:
			self.actionViewSearch.setChecked(False)
			self.searchFrame.hide()

	def viewDetailsFrame(self, view=False):
		if view or self.actionDetails.isChecked():
			try:
				self.detailsFrame.show()
				self.actionDetails.setChecked(True)
			except AttributeError:
				self.actionDetails.setChecked(False)
		else:
			self.actionDetails.setChecked(False)
			self.detailsFrame.hide()


	def setEditFrame(self, frame):
		"""Sets the frame passed in as the edit frame.
		must already be in the splitter by calling
		addEditFrames"""

		self.editFrame.hide()
		self.editFrame = frame
		self.editFrame.show()
		self.viewEditFrame(True)


	def addEditFrames(self, *listOfFrames):
		"""Adds a list of frames to the splitter at the
		right location. Shows the first one added"""
		for frame in listOfFrames:
			self.mainSplitter.insertWidget(0, frame)
			frame.hide()

		self.editFrame = listOfFrames[0]
		self.editFrame.show()
		self.viewEditFrame(True)

	def setSearchFrame(self, frame):
		self.searchFrame = frame
		loc = self.horizSplitter.indexOf(frame)
		if loc != -1:
			self.horizSplitter.widget(loc).show()
			return

		self.horizSplitter.insertWidget(0, frame)

		self.viewSearchFrame(True)


	def setDetailsFrame(self, frame):
		self.detailsFrame = frame
		loc = self.horizSplitter.indexOf(frame)
		if loc != -1:
			self.horizSplitter.widget(loc).show()
			return

		if self.horizSplitter.count > 0:
			self.horizSplitter.insertWidget(1, frame)
		else:
			self.horizSplitter.insertWidget(0, frame)

		self.viewDetailsFrame(True)

	def updateViewMenu(self):
		print "here"
		if self.searchFrame.isVisible():
			self.actionViewSearch.setChecked(True)
		else:
			self.actionViewSearch.setChecked(False)
		if self.editFrame.isVisible():
			self.actionInformation.setChecked(True)
		else:
			self.actionInformation.setChecked(False)